package tree.递归;

import java.util.List;

/**
 * @author songZiHao
 * @version 1.0.0
 * @ClassName 多叉树的最大深度559.java
 * @Description https://leetcode-cn.com/problems/maximum-depth-of-n-ary-tree/
 * @createTime 2021年06月15日 19:54:00
 */
public class 多叉树的最大深度559 {


    public int maxDepth(Node root) {
        return calculate(root);
    }

    private int calculate(Node node) {
        if (node == null) {
            return 0;
        }
        //如果没有儿子也返回，结果为1
        if (node.children.isEmpty()){
            return 1;
        }
        int maxChild=0;
        for (Node child : node.children) {
            int calculate = calculate(child);
            maxChild = Math.max(calculate, maxChild);
        }
        return maxChild+1;
    }
}

class Node {
    public int val;
    public List<Node> children;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, List<Node> _children) {
        val = _val;
        children = _children;
    }
};